package com.etc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import com.etc.dao.CustomerDao;
import com.etc.entity.Customer;
import com.etc.entity.Discuss;
import com.etc.entity.Games;
import com.etc.util.DBUtil;
import com.etc.entity.OrderMsg;

import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao{
	/**
	 * 通过电话和密码查询用户
	 */
	@Override
	public Customer findByMobileAndPassword(String mobile, String password) {
		ResultSet rs = DBUtil.query("select customer_id, nickname, mobile, password, status, image from customer where mobile = ? and password = ?", mobile, password);
		Customer customer = null;
		try {
			while (rs.next()) {
				Integer customerId = rs.getInt("customer_id"); 
				String nickname = rs.getString("nickname");       
				Integer status = rs.getInt("status");     
				String image = rs.getString("image");      
				
				// 将每一条记录封装成一个Java对象
				customer = new Customer();
				customer.setCustomerId(customerId);
				customer.setNickname(nickname);
				customer.setMobile(mobile);
				customer.setPassword(password);
				customer.setStatus(status);
				customer.setImage(image);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return customer;
	}
	
	
	
	
	@Override
	public Customer search(int id) {
		Customer customer = null;
		String sql="select * from customer where customer_id=?";
		ResultSet rs =  DBUtil.query(sql.toString(),id);
		try {
			while (rs.next()) {
			
				
				Integer ids=	rs.getInt("customer_id");
				String nickname=rs.getString("nickname");
				String mobile=rs.getString("mobile");
				String password=rs.getString("password");
				Integer status=rs.getInt("status");
				String image=rs.getString("image");

				customer=new Customer();
				customer.setCustomerId(ids);
				customer.setNickname(nickname);
				customer.setMobile(mobile);
				customer.setPassword(password);
				customer.setStatus(status);
				customer.setImage(image);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return customer;
	}
	@Override
	public int update(String nickname, String mobile, String password,int id) {
		// TODO Auto-generated method stub
		return DBUtil.update("update customer set nickname=?,mobile=?,password=? where customer_id=?", nickname,mobile,password,id);
	}

	@Override
	public List<OrderMsg> searchOrder(int id,int status) {
		OrderMsg orderMsg = null;
		List<OrderMsg> list=new ArrayList<OrderMsg>();
		String sql="select s.games_name,s.image,s.price,oi.order_no,s.`status`,oi.pay_time\r\n"
				+ "from games s\r\n"
				+ "inner join order_detail  od on  s.games_id =od.games_id\r\n"
				+ "join order_info oi on  od.order_no=oi.order_no\r\n"
				+ "where oi.customer_id=? and oi.`status`=?";
		ResultSet rs =  DBUtil.query(sql.toString(),id,status);
		try {
			while (rs.next()) {

				orderMsg=new OrderMsg();
				orderMsg.setOrder_no(rs.getString("order_no"));
				orderMsg.setPrice(rs.getBigDecimal("price"));
				orderMsg.setGamesName(rs.getString("games_name"));
				orderMsg.setStatus(rs.getInt("status"));
				orderMsg.setImage(rs.getString("image"));
				orderMsg.setNumber(1);
				orderMsg.setPay_time(rs.getString("pay_time") );
				
				list.add(orderMsg);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Games> searchGames(int id) {
		Games games = null;
		List<Games> list=new ArrayList<Games>();
		String sql="select s.games_id,s.games_name,s.price,s.type_id,s.status,s.image,s.content,s.business_id\r\n"
				+ "from games s\r\n"
				+ "join collection c on s.games_id =c.games_id\r\n"
				+ "join customer cus on cus.customer_id=c.customer_id\r\n"
				+ "where c.customer_id=?";
		ResultSet rs =  DBUtil.query(sql.toString(),id);
		try {
			while (rs.next()) {

				games=new Games();
				games.setContent(rs.getString("content"));
				games.setGamesId(rs.getInt("games_id"));
				games.setImage(rs.getString("image"));
				games.setPrice(rs.getBigDecimal("price"));
				games.setGamesName(rs.getString("games_name"));
				list.add(games);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public int del(int id) {
		// TODO Auto-generated method stub
		return DBUtil.update("delete from collection where games_id = ?", id);
	}
	@Override
	public int updatePassword(String querenmima, int id) {
		return DBUtil.update("update customer set password=? where customer_id=?",querenmima,id);
		
	}
	@Override
	public List<Discuss> searchDiscuss(int id) {
		Discuss discuss = null;
		List<Discuss> list=new ArrayList<Discuss>();
		String sql="select s.games_name,s.image,d.discuss_time,d.content\r\n" + 
				"from games s\r\n" + 
				"join discuss d on d.games_id = s.games_id\r\n" + 
				"join customer cus on d.customer_id=cus.customer_id\r\n" + 
				"where cus.customer_id=?";
		ResultSet rs =  DBUtil.query(sql.toString(),id);
		try {
			while (rs.next()) {

				discuss=new Discuss();
				Games games=new Games();
				games.setImage(rs.getString("image"));
				
				games.setGamesName(rs.getString("games_name"));
				
				discuss.setGames(games);
				discuss.setContent(rs.getString("content"));
				discuss.setDiscussTime(rs.getString("discuss_time"));
				list.add(discuss);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<OrderMsg> searchOrderChao(int id, int status) {
		OrderMsg orderMsg = null;
		List<OrderMsg> list=new ArrayList<OrderMsg>();
		String sql="select s.games_name,s.image,s.price,oi.order_no,oi.`status`,oi.pay_time\r\n"
				+ "from games s\r\n"
				+ "inner join order_detail  od on  s.games_id =od.games_id\r\n"
				+ "join order_info oi on  od.order_no=oi.order_no\r\n"
				+ "where oi.customer_id=? and oi.`status`=?";
		ResultSet rs =  DBUtil.query(sql.toString(),id,status);
		try {
			while (rs.next()) {

				orderMsg=new OrderMsg();
				orderMsg.setOrder_no(rs.getString("order_no"));
				orderMsg.setPrice(rs.getBigDecimal("price"));
				orderMsg.setGamesName(rs.getString("games_name"));
				orderMsg.setStatus(rs.getInt("status"));
				orderMsg.setImage(rs.getString("image"));
				orderMsg.setNumber(1);
				orderMsg.setPay_time(rs.getString("pay_time") );
				
				list.add(orderMsg);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 将加入用户信息添加于数据库
	 */
	public int insert(Customer customer) {
		return DBUtil.update("insert into customer(nickname,password,mobile,status)values(?,?,?,?)",
				customer.getNickname(),
				customer.getPassword(),
				customer.getMobile(),
				customer.getStatus()
				);
	}
/**
 * 查询名字在数据库是否存在
 */
	@Override
	public Customer findByNickname(String nickname) {
		// TODO Auto-generated method stub
		Customer customer=null;
				//发送SQL语句
				ResultSet rs= DBUtil.query("select customer_id,mobile,nickname,status,password,image from customer where nickname=?", nickname);
		try {
			while (rs.next()) {
				Integer customerId = rs.getInt("customer_id"); 
				String mobile = rs.getString("mobile");       
				Integer status = rs.getInt("status");     
				String password= rs.getString("password");
				String image = rs.getString("image"); 
			//将每一天数据封装进行一个java对象
				customer  = new Customer();
				customer.setNickname(nickname);
				customer.setMobile(mobile);
				customer.setCustomerId(customerId);
				customer.setStatus(status);
				customer.setImage(image);
				customer.setPassword(password);
				
 			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return customer;
	}
	/**
	 * 查询
	 */
	@Override
	public Customer findByMobile(String mobile) {
		// TODO Auto-generated method stub
		Customer customer=null;
		//发送SQL语句
		ResultSet rs=DBUtil.query("select customer_id,mobile,nickname,status,password,image from customer where mobile=?",mobile);
		try {
			while (rs.next()) {
		Integer customerId=rs.getInt("customer_id");
		String nickname=rs.getString("nickname");
		Integer status=rs.getInt("status");
		String image=rs.getString("image");
		String password=rs.getString("password");
		//将每条记录进行封装
	      customer=new Customer();
	      customer.setCustomerId(customerId);
	      customer.setNickname(nickname);
	      customer.setStatus(status);
	      customer.setImage(image);
	      customer.setPassword(password);
	      customer.setMobile(mobile);	
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return customer;
	}

}
